package com.fantasy.dao.impl;

import com.fantasy.dao.BaseDao;
import com.fantasy.dao.UserDao;
import com.fantasy.entity.Admin;
import com.fantasy.entity.User;

import java.util.Date;
import java.util.List;


public class UserDaoImpl extends BaseDao implements UserDao {


    @Override
    public int insert(User user) {
        //INSERT INTO `user`(`id`,`name`,`password`,`phone`,`headimg`,`email`,`state`,`vip`,`registerdate`,`introduce`)VALUES()
        String sql ="INSERT INTO `user`(`name`,`password`,`phone`,`headimg`,`email`,`state`,`vip`,`registerdate`,`introduce`)VALUES(?,?,?,?,?,?,?,?,?)";
        return super.update(sql,user.getName(),user.getPassword(),user.getPhone(),user.getHeadImg(),user.getEmail(),user.getState(),user.getVip(),new Date(),user.getIntroduce());
    }

    @Override
    public int delete(int id) {
        String sql = "DELETE FROM `user` WHERE id = ?;";
        return super.update(sql,id);
    }

    @Override
    public int update(User user) {
        String sql = "update `user` SET `name`=?,`password`=?,`phone`=?,`headimg`=?,`email`=?,`introduce`=? where id = ? ";
        return super.update(sql,user.getName(),user.getPassword(),user.getPhone(),user.getHeadImg(),user.getEmail(),user.getIntroduce(),user.getId());
    }

    @Override
    public User queryById(int id) {
        String sql = "select * from `user` WHERE id =?";
        return super.queryForOne(User.class,sql,id);
    }

    @Override
    public User queryByPhoneAndPassword(String phone, String password) {
        String sql = "select * from `user` WHERE phone = ? AND password = ?";
        return super.queryForOne(User.class,sql,phone,password);
    }

    @Override
    public User queryByPhone(String phone) {
        String sql = "select * from `user` WHERE phone = ?";
        return super.queryForOne(User.class,sql,phone);
    }

    @Override
    public List<User> queryAll() {
        String sql = "select * from `user`";
        return super.queryForList(User.class,sql);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql = "SELECT COUNT(1) FROM `user`";
        Number count = (Number) super.queryForSingleValue(sql);
        return count.intValue();
    }

    @Override
    public List<User> queryForPageItems(int begin, int pageSize) {
        String sql = "SELECT * FROM `user` LIMIT ?, ?";
        return super.queryForList(User.class,sql,begin,pageSize);
    }
}
